
<template>
  <div class="">
    <el-card>
      <!-- 搜索栏 -->
      <div class="clearfix">
        <el-row :gutter="20">
          <el-col :span="16">
            <el-form
              :inline="true"
              :model="requestParameters"
              ref="requestParameters"
            >
              <el-form-item prop="title">
                <strong>学科名称</strong>
                <el-input
                  @keyup.enter="handleFilter"
                  style="width: 200px"
                  :placeholder="$t('table.search')"
                  class="filter-item search"
                  v-model="requestParameters.title"
                ></el-input>
                <el-button @click="ClearContents">清除</el-button>
                <el-button
                  class="filter-item"
                  size="small"
                  type="primary"
                  icon="el-icon-search"
                  @click="searchSimple(requestParameters.title)"
                  >搜索</el-button
                >
              </el-form-item>
            </el-form>
          </el-col>
          <!--新增学科  -->
          <el-col :span="8">
            <el-button
              style="float: right; font-size: 12px"
              type="success"
              icon="el-icon-edit"
              @click="increaseSubjects"
              >新增学科</el-button
            ></el-col
          >
        </el-row>
      </div>
      <!-- 警告 -->
      <el-alert type="info" show-icon>数据共{{ total }}条 </el-alert>
      <!-- 列表 -->
      <el-table :data="tableList" border style="width: 100%">
        <el-table-column type="index" label="序号" width="50">
        </el-table-column>
        <el-table-column prop="subjectName" label="学科名称" width="180">
        </el-table-column>
        <el-table-column prop="username" label="创建者" width="180">
        </el-table-column>
        <el-table-column prop="SubjectsAddDate" label="创建日期">
          <template slot-scope="{ row }">
            <span>{{ row.addDate | parseTimeByString("{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="isFrontDisplay" label="前台是否显示">
          <template slot-scope="{ row }">
            <span>{{ row.isFrontDisplay ? "是" : "否" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录">
        </el-table-column>
        <el-table-column prop="tags" label="标签"> </el-table-column>
        <el-table-column prop="totals" label="题目数量"> </el-table-column>
        <el-table-column prop="" label="操作" width="220">
          <template slot-scope="{ row }">
            <el-button type="text" size="small" @click="directorysSubjects(row.id)"
              >学科分类</el-button
            >
            <el-button type="text" size="small" @click="tagSubjects(row.id)"
              >学科标签</el-button
            >
            <el-button type="text" size="small" @click="editSubjects(row)"
              >修改</el-button
            >
            <el-button type="text" size="small" @click="delSubject(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页按钮 -->
      <div class="pagination">
        <div class="pages">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="Number(requestParameters.page)"
            :total="Number(total)"
            :page-size="Number(requestParameters.pagesize)"
            :page-sizes="[5, 10, 20]"
            layout="prev,pager, next,sizes,jumper"
            style="float: right"
          ></el-pagination>
        </div>
      </div>
    </el-card>
    <subjects-add
      :row="row"
      :edit-id="editId"
      ref="subjectForm"
      :dialog-form-visible.sync="dialogFormVisible"
      @succeed="changeAdd"
    ></subjects-add>
  </div>
</template>

<script>
import { SubjectsList, SubjectsSimple, SubjectsRemove } from '@/api/hmmm/subjects.js'
import SubjectsAdd from '@/module-hmmm/components/subjects-add.vue'
// import router from 'router'
export default {
  name: 'Subjects',
  components: {
    SubjectsAdd
  },
  data () {
    return {
      requestParameters: {
        page: 1,
        pagesize: 10,
        title: this.title
      },
      total: null,
      dialogFormVisible: false,
      tableList: [],
      editId: '',
      row: null
    }
  },
  created () {
    this.SubjectsList()
  },
  methods: {
    changeAdd () {
      this.SubjectsList()
    },

    // 数据列表
    async SubjectsList (val) {
      const { data } = await SubjectsList(val)
      this.tableList = data.items
      this.total = data.counts
    },

    // 搜索信息
    handleFilter () {
      this.requestParameters.page = 1
      this.searchSimple(this.requestParameters.title) // 这里有问题
      this.requestParameters.title = ''
    },
    // 清除搜索信息
    ClearContents () {
      this.requestParameters.title = ''
      this.SubjectsList()
    },

    // 重置
    searchList () {
      this.$refs.requestParameters.resetFields()
    },
    // 搜索
    async searchSimple (val) {
      const { data } = await SubjectsSimple(val)
      for (const key in data) {
        if (data[key].label === val) {
          this.SubjectsList({ subjectName: data[key].label })
        }
      };
    },

    // 添加学科
    async increaseSubjects () {
      this.dialogFormVisible = true
      this.editId = ''
    },

    // 修改学科
    editSubjects (row) {
      this.editId = row.id
      this.row = row
      this.dialogFormVisible = true
      setTimeout(() => {
        this.$refs.subjectForm.SubjectsUpdate()
      }, 0)
    },
    // 删除学科
    async delSubject (row) {
      try {
        await this.$confirm('确认删除该角色吗', {
          type: 'warning'
        }).then(async () => {
          await SubjectsRemove(row)
          this.$message({
            message: '删除成功',
            type: 'success',
            showClose: true,
            duration: 3000
          })
          this.SubjectsList()
        }).catch(() => {
          this.$message({
            message: '取消',
            type: 'info',
            showClose: true,
            duration: 3000
          })
        })
      } catch (error) {
        console.error(error)
      }
    },
    // 学科分类/学科目录
    directorysSubjects (id) {
      this.$router.push({ path: './directorys', query: { id } })
    },
    // 学科标签
    tagSubjects (id) {
      this.$router.push({ path: './tags', query: { id } })
    },
    // 每页显示信息条数
    handleSizeChange (val) {
      this.requestParameters.pagesize = val
      this.SubjectsList({ pagesize: val })
    },
    // 进入某一页
    handleCurrentChange (val) {
      this.SubjectsList({ page: val })
      this.requestParameters.page = val
    }
  }

}

</script>

<style lang="less" scoped>
.search {
  margin: 0 10px;
}
.el-alert {
  margin-bottom: 20px;
}
</style>
